home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / rbput.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  3KB  |  89 lines

  1. /*****************************************************************************
  2.    MODULE: rbput.c
  3.   PURPOSE: recio character delimited integral number output functions
  4. COPYRIGHT: (C) 1994 William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.02
  8.   RELEASE: May 5, 1994
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. extern int _rstatus(REC *rp, int mode);
  18. extern int _rputc(REC *rp, int ch);
  19.  
  20. #define rfp(rp)          ((rp)->r_fp)
  21. #define rcol(rp)         ((rp)->r_colno)
  22. #define rfldch(rp)       ((rp)->r_fldch)
  23.  
  24. #define ulong            unsigned long
  25. #define UNSIGNED         0
  26. #define SIGNED           1
  27.  
  28. /****************************************************************************/
  29. static int                   /* return error status (0=no error; !0=error)  */
  30.     _rbputl(                 /* output integral number to record stream     */
  31.         REC *rp,             /* pointer to record stream                    */
  32.         int  base,           /* base (radix) (2 to 36)                      */
  33.         int  sign,           /* signed number (1=signed; 0=unsigned)        */
  34.         long num)            /* number to put to stream                     */
  35. /****************************************************************************/
  36. {
  37.     int err=EOF;             /* return error (0=no error; !0=error) */
  38.     if (!_rstatus(rp, R_WRITE)) { 
  39.         if (base >= 2 && base <= 36) { 
  40.             rfldno(rp)++; 
  41.             /* if not first field, put field separator */
  42.             if (rfldno(rp) > 1) { 
  43.                 err = _rputc(rp, rfldch(rp)); 
  44.                 if (err) goto done; 
  45.             }
  46.             if (sign) {
  47.                 ltoa(num, _r_nsbuf, base);
  48.             } else {
  49.                 ultoa((ulong)num, _r_nsbuf, base);
  50.             }
  51.             err = fputs(_r_nsbuf, rfp(rp)); 
  52.             if (err==EOF) { 
  53.                 rseterr(rp, R_ENOPUT); 
  54.                 goto done; 
  55.             } else { 
  56.                 rcol(rp) += strlen(_r_nsbuf); 
  57.                 err = 0; 
  58.                 goto done; 
  59.             } 
  60.         } 
  61.         rseterr(rp, R_EINVAL); 
  62.     } 
  63. done: 
  64.     return err; 
  65. }
  66.  
  67. /****************************************************************************/
  68. /* character delimited integral number output functions                     */
  69. /****************************************************************************/
  70. int rbputi(REC *rp, int base, int num)
  71. {
  72.     return _rbputl(rp, base, SIGNED, (long) num);
  73. }
  74.  
  75. int rbputui(REC *rp, int base, unsigned int num)
  76. {
  77.     return _rbputl(rp, base, UNSIGNED, (long) num);
  78. }
  79.  
  80. int rbputl(REC *rp, int base, long num)
  81. {
  82.     return _rbputl(rp, base, SIGNED, num);
  83. }
  84.  
  85. int rbputul(REC *rp, int base, unsigned long num)
  86. {
  87.     return _rbputl(rp, base, UNSIGNED, (long) num);
  88. }
  89.